# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from scrapy.loader import ItemLoader
from ..neteaseItem2 import NetEaseItem

class Netease2Spider(CrawlSpider):
    name = 'netease2'
    allowed_domains = ['163.com']
    # urls = 'https://163.com/'
    # start_urls = urls.split(',')
    rules = (
        Rule(LinkExtractor(allow=r'(\w+):\/\/([^/:]+)\/(\d{2})+\/(\d{4})+\/(\d{2})+\/([^#]*)'), callback='parse_item', follow=True),
    )

    def __init__(self,urls='https://163.com/',*args,**kwargs):   
        super(Netease2Spider,self).__init__(*args,**kwargs) 
        self.urls = urls
        self.start_urls = urls.split(',')

    def parse_item(self, response):
        #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
        #item['name'] = response.xpath('//div[@id="name"]').get()
        #item['description'] = response.xpath('//div[@id="description"]').get()
        loader = ItemLoader(item = NetEaseItem(), response=response)
        loader.add_css('title', '#epContentLeft>h1::text')
        loader.add_css('pub_date', '#epContentLeft .post_time_source::text')
        loader.add_css('desc', '#epContentLeft .post_desc::text')
        
        # 游戏栏目 play.163.com
        loader.add_css('title', 'h1.article-h1::text')
        loader.add_css('desc', '.artical-summary::text')

        # https://sports.163.com/20/0306/06/F712SBHK0005877U.html
        loader.add_css('title','.atc_title>h1::text')
        loader.add_css('pub_date','.ep-time-soure::text')

        loader.add_css('body', '#endText::text')
        loader.add_value('link',response.url)
        yield loader.load_item()
